//loads the tags according to the dictionary selected by the user.
function loadTags()
{
	dictType = document.configure.dictionary.options[document.configure.dictionary.selectedIndex].value;
	
	var oTextBox = document.getElementById('tbox');
	var oDropDown = document.getElementById('cbox');
	if(dictType==0)
	{
		oDropDown.style.display = "none";
		oTextBox.style.display = "";
	}else{
		oDropDown.style.display = "";
		oTextBox.style.display = "none";
	}
	
	//remove all tags
	for(i=document.configure.tags.options.length-1; i>0 ; i--)
	{
		document.configure.tags.options[i] = null;
	}
	var tags = new Array() ; 
	switch (dictType)
	{
		case '1':
			tags = (document.frmData.personalDict.value).split(",");
			break;
		case '2':
			tags = (document.frmData.businessDict.value).split(",");
			break;
		case '3':
			tags = (document.frmData.communityDict.value).split(",");
			break;
		case '0':
			break;
			
		default : break;
	}
	
	for(i=0 ;i<tags.length; i++)
		document.configure.tags.options[i] = new Option(tags[i], tags[i]);
		
	document.configure.ForwardsTo.value = "http://";	
}

function editForward(iLink, url, public, editRowId)
{
	iLink = iLink.slice(15);
	iname = iLink.slice(0,iLink.indexOf('/'));

	tag = iLink.slice(iLink.indexOf('/')+1);

	tag=tag.replace('(','');
	tag=tag.substr(0,tag.length-1);

	//Search  and select I-Name
	for (i=0;i<document.configure.iname.options.length; i++)
	{
		if (document.configure.iname.options[i].text == trim(iname))
		{
			document.configure.iname.options[i].selected = true;
			break;
		}
	}		
	//document.configure["iname"].disabled = true;
	
	//Search and select Tag
	if(document.frmData.personalDict.value.indexOf(tag) > -1)
	{
		document.configure.dictionary.selectedIndex = 0;
		loadTags();
	}
	
	else if(document.frmData.businessDict.value.indexOf(tag) > -1)
		{
		document.configure.dictionary.selectedIndex = 1;
		loadTags();
		}
	else if(document.frmData.communityDict.value.indexOf(tag) > -1)
		{
		document.configure.dictionary.selectedIndex = 2;
		loadTags();
		}
	else
	{
		document.configure.dictionary.selectedIndex = 3;
		loadTags();
		document.configure.TagTextBox.value = tag;
		//return;
	}
	
	for (i=0;i<document.configure.tags.options.length; i++)	
	{
		if (document.configure.tags.options[i].text == tag)
		{
			document.configure.tags.options[i].selected = true;
			break;
		}
	}
	
	//Set value of Forwards TO
	document.configure.ForwardsTo.value=url;
	      
	if(public == 'true')
		document.configure.ispublic.selectedIndex=0;
	else
		document.configure.ispublic.selectedIndex=1;	

	//Set values in edit IFrame
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
	{
	 window.frames['ifrEdit'].Forwards.oldIname.value=iname;
 	 window.frames['ifrEdit'].Forwards.oldTag.value=tag;
 	 window.frames['ifrEdit'].Forwards.isPublic.value=public; 	 
 	 window.frames['ifrEdit'].Forwards.opType.value=2;
	}
	else
	{
	 window.frames['ifrEdit'].document.getElementById("Forwards").oldIname.value=iname;
	 window.frames['ifrEdit'].document.getElementById("Forwards").oldTag.value=tag;
	 window.frames['ifrEdit'].document.getElementById("Forwards").isPublic.value=public;
	 window.frames['ifrEdit'].document.getElementById("Forwards").opType.value=2;
	}
	
	document.frmData.opType.value=2;
}

function delForward(iLink, rowId)
{
	var msg = document.messages.del_ilink.value;
	var blnConfirmChanges= window.confirm(msg);
	
	if(!blnConfirmChanges)
		return;
		
	iLink = iLink.slice(15);
	iname = iLink.slice(0,iLink.indexOf('/'));

	tag = iLink.slice(iLink.indexOf('/')+1);

	tag=tag.replace('(','');
	tag=tag.substr(0,tag.length-1);
	
	//Set values in del IFrame
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
	{
	 window.frames['ifrDelete'].Forwards.iname.value=iname;
 	 window.frames['ifrDelete'].Forwards.tag.value=tag;
 	 window.frames['ifrDelete'].Forwards.opType.value=3;
 	 window.frames['ifrDelete'].Forwards.submit();
 	 var oRow = window.frames['ifrLinks'].document.getElementById(rowId);
 	 oRow.parentNode.removeChild(oRow);	 
	}
	else
	{
	 window.frames['ifrDelete'].document.getElementById("Forwards").iname.value=iname;
	 window.frames['ifrDelete'].document.getElementById("Forwards").tag.value=tag;
	 window.frames['ifrDelete'].document.getElementById("Forwards").opType.value=3;
	 window.frames['ifrDelete'].document.getElementById("Forwards").submit();
	 var oRow = window.frames['ifrLinks'].document.getElementById(rowId);
	 oRow.parentNode.removeChild(oRow);	 
	}
	resetPage();
	resizeIframe();
}

function createForward()
{
	var tagValue;
	if(document.configure.TagTextBox.style.display=='none')
	{
		tagValue=trim(document.configure.tags.options[document.configure.tags.selectedIndex].value);
	}
	else
	{
		tagValue=trim(document.configure.TagTextBox.value);
	}
	
	//Check if Tag value is empty
	if(tagValue == "")
	{
		alert(document.messages.tag_empty.value);
		return;
	}
	
	//Check if tag value contains spaces
	if(tagValue.indexOf(' ') != -1 || tagValue.indexOf('\'') != -1 || tagValue.indexOf('\\') != -1)
	{
		alert(document.messages.tag_spaces.value);
		return;
	}
	
	var tmpTag = tagValue;
	if(tmpTag.toLowerCase() == "+contact" || tmpTag  == "(+contact)")
	{
		alert(document.messages.contact_standard.value);
		return;
	}
		
	if(trim(document.configure.ForwardsTo.value) == "")
	{
		alert(document.messages.fwd_empty.value)
		return;
	}
	
	var accessValue=document.configure.ispublic.options[document.configure.ispublic.selectedIndex].value;
	if(trim(accessValue.toUpperCase()) == 'PUBLIC')
		accessValue = 'true';
	else
		accessValue = 'false';
	
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
	{ 
	 window.frames['ifrCreate'].Forwards.iname.value=document.configure.iname.options[document.configure.iname.selectedIndex].value;
 	 window.frames['ifrCreate'].Forwards.tag.value=tagValue;
 	 window.frames['ifrCreate'].Forwards.webResource.value=document.configure.ForwardsTo.value;
 	 window.frames['ifrCreate'].Forwards.isPublic.value=accessValue;
 	 window.frames['ifrCreate'].Forwards.opType.value=1;
 	 window.frames['ifrCreate'].Forwards.submit();
	}
	else
	{
	 window.frames['ifrCreate'].document.getElementById("Forwards").iname.value=document.configure.iname.options[document.configure.iname.selectedIndex].value;
	 window.frames['ifrCreate'].document.getElementById("Forwards").tag.value=tagValue;
	 window.frames['ifrCreate'].document.getElementById("Forwards").webResource.value=document.configure.ForwardsTo.value;
 	 window.frames['ifrCreate'].document.getElementById("Forwards").isPublic.value=accessValue;
	 window.frames['ifrCreate'].document.getElementById("Forwards").opType.value=1;
	 window.frames['ifrCreate'].document.getElementById("Forwards").submit();
	}
}

function selectOp()
{
	if(document.frmData.opType.value==1)
	{
		createForward();
	}
	else
	{
		var tagValue;
		
		if(document.configure.TagTextBox.style.display=='none')
		{tagValue=document.configure.tags.options[document.configure.tags.selectedIndex].value;	}
		else
		{tagValue=document.configure.TagTextBox.value;}
		
		//Check if Tag value is empty
		if(tagValue == "")
		{
			alert(document.messages.tag_empty.value);
			return;
		}
		
		//Check if tag value contains spaces
		if(tagValue.indexOf(' ') != -1)
		{
			alert(document.messages.tag_spaces.value);
			return;
		}
	
		var tmpTag = tagValue;
		if(tmpTag  == "+contact" || tmpTag  == "(+contact)")
		{
			alert(document.messages.contact_standard.value);
			return;
		}
			
		if(trim(document.configure.ForwardsTo.value) == "")
		{
			alert(document.messages.fwd_empty.value)
			return;
		}
		
		var accessValue=document.configure.ispublic.options[document.configure.ispublic.selectedIndex].value;
		if(trim(accessValue.toUpperCase()) == 'PUBLIC')
			accessValue = 'true';
		else
			accessValue = 'false';
		   		
		if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
		{ 
		 window.frames['ifrEdit'].Forwards.iname.value=document.configure.iname.options[document.configure.iname.selectedIndex].value;
	 	 window.frames['ifrEdit'].Forwards.tag.value=tagValue;
	 	 window.frames['ifrEdit'].Forwards.webResource.value=document.configure.ForwardsTo.value;
 	 	 window.frames['ifrEdit'].Forwards.isPublic.value=accessValue;
	 	 window.frames['ifrEdit'].Forwards.opType.value=2;
	 	 window.frames['ifrEdit'].Forwards.submit();
		}
		else
		{
		 window.frames['ifrEdit'].document.getElementById("Forwards").iname.value=document.configure.iname.options[document.configure.iname.selectedIndex].value;
		 window.frames['ifrEdit'].document.getElementById("Forwards").tag.value=tagValue;
		 window.frames['ifrEdit'].document.getElementById("Forwards").webResource.value=document.configure.ForwardsTo.value;
 		 window.frames['ifrEdit'].document.getElementById("Forwards").isPublic.value=accessValue;
		 window.frames['ifrEdit'].document.getElementById("Forwards").opType.value=2;
		 window.frames['ifrEdit'].document.getElementById("Forwards").submit();
		}
	}
}

function resizeIframe()
{
	var currentfr=document.getElementById("ifrLinks");

	if (currentfr && !window.opera)
	{
		currentfr.style.display="block";

		if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
			currentfr.height = currentfr.contentDocument.body.offsetHeight+20;
		else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
			currentfr.height = currentfr.Document.body.scrollHeight;
	
		if (currentfr.addEventListener)
			currentfr.addEventListener("load", readjustIframe, false)
		else if (currentfr.attachEvent)
		{
			currentfr.detachEvent("onload", readjustIframe) // Bug fix line
			currentfr.attachEvent("onload", readjustIframe)
		}
	}
}


function readjustIframe(loadevt)
{
	var crossevt=(window.event)? event : loadevt
	var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
	if (iframeroot)
	resizeIframe(iframeroot.id);
}

function resetPage()
{
	document.frmData.opType.value=1;
	document.configure.TagTextBox.value = "";
	document.configure.ForwardsTo.value="http://";	
	document.configure.iname.options[0].selected = true;
	document.configure.tags.options[0].selected = true;
	document.configure.ispublic.options[0].selected = true;
}

function trim(str)
{
	while (str.substring(0,1) == ' ')
    	str = str.substring(1,str.length);
    
  	
  	while (str.substring(str.length-1,str.length) == ' ') 
    	str = str.substring(0,str.length-1);

	  return str;
}

function focusOnEdit()
{
	scroll(0,0);
	if(document.getElementById('tbox').style.display=="none")
		document.getElementById('cbox').focus();
	else
		document.getElementById('tbox').focus();
}

function unescapeHTML(html) {
  	    var htmlNode = document.createElement("DIV");
  	    htmlNode.innerHTML = html;
  	         if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
  	            return htmlNode.innerText;
  	         else
  	            return htmlNode.innerHTML;
}

